package com.cljz.makepassenger.ui.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.event.CoverPicEvent;
import com.cljz.makepassenger.event.UserInfoChangeEvent;
import com.cljz.makepassenger.present.activity.CutViewP;
import com.cljz.makepassenger.utils.ActivityManager;
import com.cljz.makepassenger.utils.BitmapUtils;
import com.cljz.makepassenger.utils.FileUtils;
import com.cljz.makepassenger.widget.cutview.ClipImageLayout;
import com.jakewharton.rxbinding.view.RxView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.event.BusProvider;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by apple on 2017/4/12.
 */

public class CutViewActivity extends BaseActivity<CutViewP> {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.cil_cut)
    ClipImageLayout cilCut;
    @BindView(R.id.ll_main)
    LinearLayout llMain;
    @BindView(R.id.fl_title_right)
    FrameLayout flTitleRight;

    public static final String CUT_SCALE = "CUT_SCALE";
    private int[] picScale;

    @Override
    public void initData(Bundle savedInstanceState) {
        Bundle bundle = getIntent().getExtras();
        tvTitleName.setText("剪裁图片");
        tvTitleRight.setText("剪裁");
        cilCut.setHorizontalPadding(20);
        picScale = bundle.getIntArray(CUT_SCALE);
        if (picScale != null) {
            cilCut.setProportion(picScale[0], picScale[1]);
        } else {
            cilCut.setProportion(1, 1);
        }
        cilCut.setImageDrawable(bundle.getString("path"));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_cut_view;
    }

    @Override
    public CutViewP newP() {
        return new CutViewP();
    }

    @Override
    protected void initListener() {
        super.initListener();
        RxView.clicks(flTitleRight)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (picScale != null && picScale[0] == 5) {
                            Bitmap bitmap = cilCut.clip();
                            File file = FileUtils.createFileInTempDirectory(System.currentTimeMillis() + ".png");
                            FileOutputStream out;
                            try {
                                out = new FileOutputStream(file);
                                if (bitmap.compress(Bitmap.CompressFormat.PNG, 90, out)) {
                                    out.flush();
                                    out.close();
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                           }
                            String bitmapPath = file.getAbsolutePath();
                            BusProvider.getBus().post(new CoverPicEvent(bitmapPath));
                            ActivityManager.getInstance().finishAllActivity(UserLogoActivity.class);
                            Router.pop(CutViewActivity.this);
                        } else {
                            showLoadingDialog();
                            getP().getUploadImg(BitmapUtils.convertPicToString(cilCut.clip()));
                        }
                    }
                });
        RxView.clicks(flBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        finish();
                    }
                });
    }

    public void getUpLoadImgFail(String error) {
        dismissLoadingDialog();
        showToast(error,R.mipmap.jinshi);
        finish();
        ActivityManager.getInstance().finishAllActivity(UserLogoActivity.class);
    }

    public void userLogoUpdate(String msg) {
        dismissLoadingDialog();
        showToast(msg,R.mipmap.nearby_success);

        BusProvider.getBus().post(new UserInfoChangeEvent());
        finish();
        ActivityManager.getInstance().finishAllActivity(UserLogoActivity.class);
    }
}
